cmake_minimum_required(VERSION 3.15)
project(myApp)

string(TIMESTAMP VERSION "%Y%m%d%H%M%S")
add_definitions(-DVERSION="${VERSION}")

# 优先使用环境变量中的编译器
find_program(CMAKE_C_COMPILER NAMES gcc.exe PATHS $ENV{Mingw}/bin NO_DEFAULT_PATH)
# find_program(CMAKE_CXX_COMPILER NAMES g++.exe PATHS $ENV{Mingw}/bin NO_DEFAULT_PATH)

# 添加头文件路径
include_directories(${CMAKE_SOURCE_DIR}/include)
include_directories(${CMAKE_SOURCE_DIR}/src/test)

# 主可执行文件（仅包含main.c）
add_executable(${PROJECT_NAME} src/main.c)

# 递归收集所有.c文件（包含子目录）
file(GLOB_RECURSE ALL_C_FILES CONFIGURE_DEPENDS "src/*.c")
# 排除main.c文件
list(FILTER ALL_C_FILES EXCLUDE REGEX "src/main\\.c")

if(NOT ALL_C_FILES STREQUAL "")
    add_library(MyLib STATIC ${ALL_C_FILES})
    target_link_libraries(${PROJECT_NAME} PRIVATE MyLib)
endif()

# 全局编译选项
set(CMAKE_CXX_FLAGS_DEBUG "-g")
set(CMAKE_CXX_FLAGS_RELEASE "-O3")

set(DEBUG_LEVEL 0 CACHE STRING "打印等级")
add_compile_definitions(DEBUG_LEVEL=${DEBUG_LEVEL})
option(DEBUG_MODE "启用DEBUG打印" OFF)  # 默认关闭
if(DEBUG_MODE)
    add_compile_definitions(DEBUG_MODE=1)
endif()
 
# 目标定义后设置编译定义
target_compile_definitions(${PROJECT_NAME} PRIVATE
    $<$<CONFIG:Debug>:DEBUG>        # Debug模式启用DEBUG宏
    $<$<CONFIG:Release>:NDEBUG>     # Release模式启用NDEBUG宏
)

# 构建类型设置
if(NOT CMAKE_BUILD_TYPE)
    set(CMAKE_BUILD_TYPE Release CACHE STRING "Build type" FORCE)
endif()

# 安装配置
include(GNUInstallDirs)
install(TARGETS ${PROJECT_NAME}
        RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
        ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
        LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR})